package com.njit.campuslifeboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.njit.campuslifeboot.entity.dto.UserDTO;
import com.njit.campuslifeboot.entity.po.User;
import com.njit.campuslifeboot.mapper.UserMapper;
import com.njit.campuslifeboot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njit.campuslifeboot.util.RandomImage;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Axin
 * @since 2022-11-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean isExist(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("USERNAME", username);
        User user = userMapper.selectOne(wrapper);
        return user != null;
    }

    @Override
    public boolean register(UserDTO user) {
        if (user == null) {
            return false;
        }
        User nowUser = new User();
        nowUser.setUsername(user.getUsername());
        nowUser.setCreateTime(LocalDateTime.now());
        nowUser.setSalt(UUID.randomUUID().toString());
        nowUser.setPassword(DigestUtils.md5DigestAsHex((nowUser.getSalt() + user.getPassword()).getBytes()));
        nowUser.setPersonName(nowUser.getUsername());
        nowUser.setHeadImage(RandomImage.getRandomImage());
        int insert = userMapper.insert(nowUser);
        return insert > 0;
    }

    @Override
    public boolean login(UserDTO user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("USERNAME", user.getUsername());
        User one = userMapper.selectOne(wrapper);
        if (one == null) {
            return false;
        }
        return DigestUtils.md5DigestAsHex((one.getSalt() + user.getPassword()).getBytes()).equals(one.getPassword());
    }

    @Override
    public boolean updateInfo(User user,User one) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", user.getId());
        if (!Objects.equals(user.getPassword(), "")) {
            String password = DigestUtils.md5DigestAsHex((one.getSalt() + user.getPassword()).getBytes());
            updateWrapper.set("PASSWORD", password);
        }
        if (!Objects.equals(user.getPersonName(), "")) {
            updateWrapper.set("PERSON_NAME", user.getPersonName());
        }
        if (!Objects.equals(user.getSchoolName(), "")) {
            updateWrapper.set("SCHOOL_NAME", user.getSchoolName());
        }
        if (user.getPersonAge() != 0) {
            updateWrapper.set("PERSON_AGE", user.getPersonAge());
        }
        if (!Objects.equals(user.getPersonAddress(), "")) {
            updateWrapper.set("PERSON_ADDRESS", user.getPersonAddress());
        }
        if (!Objects.equals(user.getHeadImage(), "")){
            updateWrapper.set("HEAD_IMAGE",user.getHeadImage());
        }
        int update = userMapper.update(user, updateWrapper);
        return update > 0;
    }

    @Override
    public List<User> getUserByIds(List<String> ids) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ID",ids);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }
}
